package ar.arcraft.core;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.bukkit.entity.Player;

import utils.database.MySQL;

public class Vip {
	public static int setVip(String vipCode, MySQL mySQL, Player player){
		if(checkVipPayment(player, mySQL, vipCode)){
			Connection c = null;
			try {
				c = mySQL.openConnection();
				Statement statement = c.createStatement(); 
				ResultSet res = statement.executeQuery("SELECT * FROM VipCommands WHERE vipCode = '"+vipCode+"'");
				while(res.next()){
					System.out.println(res.getString("command"));
					Arcraft.executeCommandAsPlayer(player, res.getString("command"));
				}
				return 2;
				
				
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return 3;
				}
		}
		return 4;
		
	}
	public static boolean checkVipPayment(Player player, MySQL mySQL, String vipCode){
		Connection c = null;
		try {
			c = mySQL.openConnection();
			Statement statement = c.createStatement();  
			ResultSet res = statement.executeQuery("select name from vipspending where name = '"+player.getName()+"' and vipCode = '"+vipCode+"'");
			
			if(!res.next()){
					return false;
			}else {
					return true;				
			}
			
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		return false;
	}
}

